package edu.nf.takeoutdemo.web.user;

import edu.nf.takeoutdemo.commons.base.BaseController;
import edu.nf.takeoutdemo.commons.properties.JwtProperties;
import edu.nf.takeoutdemo.commons.result.Result;
import edu.nf.takeoutdemo.commons.utils.JwtUtil;
import edu.nf.takeoutdemo.model.converter.UserConverter;
import edu.nf.takeoutdemo.model.dto.UserLoginDTO;
import edu.nf.takeoutdemo.model.entity.User;
import edu.nf.takeoutdemo.model.vo.UserLoginVO;
import edu.nf.takeoutdemo.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author zjl
 * @date 2025/10/27
 */
@RestController
@RequestMapping("/user/user")
@RequiredArgsConstructor
@Slf4j
public class UserController extends BaseController {
    private final UserService userService;
    private final UserConverter  userConverter;
    private final JwtProperties  jwtProperties;
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("微信用户登录：{}", userLoginDTO);
        //微信登录
        User user = userService.wxLogin(userLoginDTO);
        //为微信用户生成令牌
        Map<String,Object> claims = new HashMap<>();
        claims.put("userId", user.getId());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
        log.info("token: {}", token);
        UserLoginVO userLoginVO = userConverter.toUserLoginVO(user);
        userLoginVO.setToken(token);
        return success(userLoginVO);
    }
}
